<template>
  <div class="common-table-component">
    <a-table
      :loading="loading"
      :columns="columns"
      :data-source="data"
      :row-key="(record: ExtendedDept) => record.id"
      :pagination="false"
      :scroll="{ x: 'max-content' }"
      :expandable="{
        expandedKeys: expandedKeys,
        onExpandedRowsChange: handleExpandedRowsChange
      }"
    >
      <template #bodyCell="{ column, record }">
        <!-- 部门状态 -->
        <template v-if="column.key === 'status'">
          <div class="status-tag">
            <a-switch
              :checked="(record as ExtendedDept).status === 1"
              :loading="(record as ExtendedDept).statusLoading"
              @change="(checked) => handleStatusChange(record as ExtendedDept, checked ? 1 : 0)"
            />
          </div>
        </template>

        <!-- 创建时间 -->
        <template v-else-if="column.key === 'createTime'">
          <span>{{ formatDate((record as ExtendedDept).createTime) }}</span>
        </template>

        <!-- 操作 -->
        <template v-else-if="column.key === 'action'">
          <div class="action-column">
            <a-button type="link" size="small" @click="handleEdit(record as ExtendedDept)">
              <template #icon><EditOutlined /></template>{{ $t('common.edit') }}
            </a-button>
            <a-button type="link" size="small" @click="handleAddSub(record as ExtendedDept)">
              <template #icon><PlusOutlined /></template>{{ $t('dept.addSub') }}
            </a-button>
            <a-button type="link" size="small" danger @click="handleDelete(record as ExtendedDept)">
              <template #icon><DeleteOutlined /></template>{{ $t('common.delete') }}
            </a-button>
          </div>
        </template>
      </template>
    </a-table>
  </div>
</template>

<script lang="ts" setup>
import type { TableColumnsType } from 'ant-design-vue'
import type { ExtendedDept } from '@/types/api/system/dept'
import { formatDate } from '@/utils/format'

defineOptions({
  name: 'DeptTable'
})

withDefaults(defineProps<{
  loading?: boolean
  columns: TableColumnsType
  data: ExtendedDept[]
  expandedKeys: string[]
}>(), {
  loading: false,
  data: () => [],
  expandedKeys: () => []
})

const emit = defineEmits<{
  (e: 'edit', record: ExtendedDept): void
  (e: 'delete', record: ExtendedDept): void
  (e: 'add-sub', record: ExtendedDept): void
  (e: 'expanded-change', keys: string[]): void
  (e: 'status-change', record: ExtendedDept, status: number): void
}>()

const handleStatusChange = (record: ExtendedDept, status: number) => {
  emit('status-change', record, status)
}

const handleEdit = (record: ExtendedDept) => {
  emit('edit', record)
}

const handleDelete = (record: ExtendedDept) => {
  emit('delete', record)
}

const handleAddSub = (record: ExtendedDept) => {
  emit('add-sub', record)
}

const handleExpandedRowsChange = (keys: string[]) => {
  emit('expanded-change', keys)
}
</script>

<style lang="scss" scoped>
@import '@/assets/styles/views/common.scss';
</style> 